package org.business.accounting.beans;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.business.accounting.interfaces.Cart;
import org.business.accounting.interfaces.CartConfirmHelper;
import org.business.accounting.interfaces.Computer;
import org.business.accounting.interfaces.ComputerOrder;
import org.business.accounting.service.ComputerDAO;

public class CartConfirmHelperBean implements CartConfirmHelper {

	private Cart cart;

	private ComputerDAO computerDAO;
	
	public boolean confirm(boolean isAdmin) {
		if ( (cart == null) || (computerDAO == null) ) {
			return false;
		}
		
		if (isAdmin) {
			return confirmAdmin();
		}
		return confirmUser();
	}

	private boolean confirmUser() {
		cart.divideOrders();
		
		return updateOrders();
	}

	private boolean updateOrders() {
		Collection<ComputerOrder> compCollection = cart.getComputerOrders();
		Iterator<ComputerOrder> iterator = compCollection.iterator();
		ComputerOrder computerOrder;
		Computer computer;
		
		/*
		 * Use all names of computers
		 */
		List<String> names = new ArrayList<String>(compCollection.size());
		while (iterator.hasNext()) {
			computerOrder = iterator.next();
			computer = (Computer) computerOrder.getItem();
			if (!names.contains(computer.getName())) {
				names.add(computer.getName());
			}
		}
		
		/*
		 * Calculate ordered amount of an Product
		 */
		Iterator<String> namesIterator = names.iterator();
		double alovedAmount = 0;
		double amount = 0;
		while (namesIterator.hasNext()) {
			String name = namesIterator.next();
			
			iterator = compCollection.iterator();
			while (iterator.hasNext()) {
				computerOrder = iterator.next();
				computer = (Computer) computerOrder.getItem();
				if (computer.getName().equalsIgnoreCase(name)) {
					alovedAmount = computer.getAmount();
					amount += computerOrder.getQty();
				}
			}
			if (alovedAmount < amount) {
				return false;
			}
			alovedAmount = 0;	
			amount = 0;
		}
		
		iterator = compCollection.iterator();
		while (iterator.hasNext()) {
			computerOrder = iterator.next();
			computer = (Computer) computerOrder.getItem();
			computer.setAmount(computer.getAmount() - computerOrder.getQty());
		}
		
		return true;
	}

	private boolean confirmAdmin() {
		cart.divideOrders();
		
		return saveComputers();
	}
	
	//
	// Computer
	//
	
	private boolean saveComputers() {
		Collection<ComputerOrder> compCollection = cart.getComputerOrders();
		
		Iterator<ComputerOrder> iterator = compCollection.iterator();
		ComputerOrder computerOrder;
		Computer computer;
		Computer newComputer;
		
		while (iterator.hasNext()) {
			computerOrder = iterator.next();
			computer = (Computer) computerOrder.getItem();
			computer.setAmount(computer.getAmount() - computerOrder.getQty());
			
			Computer tmpComputer = computerDAO.get(computer.getName());
			if (tmpComputer == null) {
				newComputer = computer.clone();
				newComputer.setAmount(computerOrder.getQty());
				
				computerDAO.save(newComputer);
			} else {
				tmpComputer.setAmount(tmpComputer.getAmount() + computerOrder.getQty());
			}
		}
		
		// always will be true (for Now)
		return true;
	}
	
	//
	// Geters And seters
	//

	public Cart getCart() {
		return cart;
	}

	public void setCart(Cart cart) {
		this.cart = cart;
	}

	public ComputerDAO getComputerDAO() {
		return computerDAO;
	}

	public void setComputerDAO(ComputerDAO computerDAO) {
		this.computerDAO = computerDAO;
	}

}
